package com.gtw.modules.sys.controller;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbActivity;
import com.gtw.modules.entity.TbActivityCommodity;
import com.gtw.modules.entity.TbCommodity;
import com.gtw.modules.sys.DTO.ActivityCommodityDTO;
import com.gtw.modules.sys.service.SysCommodityService;
import com.gtw.modules.sys.service.SysTbActivityCommodityService;
import com.gtw.modules.sys.service.SysTbActivityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/** 活动controller
 * Created by wuye on 2018/2/26.
 */
@RestController
@RequestMapping("/sys/activity")
public class SysTbActivityController extends AbstractController{

    @Autowired
    private SysTbActivityService sysTbActivityService;

    @Autowired
    private SysCommodityService sysCommodityService;

    @Autowired
    private SysTbActivityCommodityService sysTbActivityCommodityService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:activity:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbActivity> activityList = sysTbActivityService.queryList(query);
        int total = sysTbActivityService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(activityList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 根据活动ID查询商品
     * @return
     */
    @RequestMapping("/foundByActivityId")
    public R foundByActivityId(@RequestParam Integer id){
        List<TbActivityCommodity> list = sysTbActivityCommodityService.foundByActivityId(id);
        List<ActivityCommodityDTO> listdto = new ArrayList<>();
        for (TbActivityCommodity tac: list) {
            TbActivity tbActivity = sysTbActivityService.queryObject(tac.getActivityid());
            TbCommodity tbCommodity = sysCommodityService.queryObject(tac.getCommodiytid());
            TbActivityCommodity tbActivityCommodity = tac;
            ActivityCommodityDTO dto = new ActivityCommodityDTO();
            dto.setTbActivity(tbActivity);
            dto.setTbCommodity(tbCommodity);
            dto.setTbActivityCommodity(tbActivityCommodity);
            listdto.add(dto);
        }
        return R.ok().put("listdto",listdto);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:activity:info")
    public R info(@PathVariable("id") Integer id){
        TbActivity activity = sysTbActivityService.queryObject(id);

        return R.ok().put("activity", activity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:activity:save")
    public R save(@RequestBody TbActivity activity){
        activity.setCreatetime(new Date());
        activity.setCreateuser(getUser().getUsername());
        activity.setIsopen(0);
        activity.setIsdelete(0);
        if(activity.getType()==2){
            activity.setThreebargain(80f);
            activity.setTenbargain(60f);
            activity.setSixbargain(70f);
        }else {
            activity.setThreebargain(0f);
            activity.setTenbargain(0f);
            activity.setSixbargain(0f);
        }
        activity.setActivitytime(60);
        sysTbActivityService.save(activity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:activity:update")
    public R update(@RequestBody TbActivity activity){
        sysTbActivityService.update(activity);

        return R.ok();
    }

    /**
     * 删除(软删除)
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:activity:delete")
    public R delete(@RequestBody Integer[] ids){

        for (Integer id:ids) {
            TbActivity tbActivity = sysTbActivityService.queryObject(id);
            tbActivity.setIsdelete(1);
            sysTbActivityService.update(tbActivity);
        }
        return R.ok();
    }


    /**
     * 添加商品(活动id，商品id,活动价，活动商品库存)
     * @return
     */
    @RequestMapping("/addCommodity")
    public R addCommodity(@RequestBody TbActivityCommodity tbActivityCommodity){
        tbActivityCommodity.setCreatedate(new Date());
        tbActivityCommodity.setCreateuser(getUser().getUsername());
        tbActivityCommodity.setIsdelete(0);
        tbActivityCommodity.setUpdatedate(new Date());
        tbActivityCommodity.setReserve(0);
        tbActivityCommodity.setUpdateuser(getUser().getUsername());
        tbActivityCommodity.setNowprice(tbActivityCommodity.getNowprice()*100);
        sysTbActivityCommodityService.save(tbActivityCommodity);
        return R.ok();
    }

    /**
     * 修改商品
     * @return
     */
    @RequestMapping("/updateCommodity")
    public R updateCommodity(@RequestParam TbActivityCommodity tbActivityCommodity){
        tbActivityCommodity.setUpdateuser(getUser().getUsername());
        tbActivityCommodity.setUpdatedate(new Date());
        tbActivityCommodity.setNowprice(tbActivityCommodity.getNowprice()*100);
        sysTbActivityCommodityService.update(tbActivityCommodity);
        return R.ok();
    }

    /**
     * 删除商品
     * @return
     */
    @RequestMapping("/delCommodity")
    public R delCommodity(@RequestBody Integer[] ids){
        for (Integer id:ids) {
            TbActivityCommodity tac = sysTbActivityCommodityService.queryObject(id);
            tac.setIsdelete(1);
            sysTbActivityCommodityService.update(tac);
        }
        return R.ok();
    }
}
